package com.higgins.lawapp.server;

import java.util.ArrayList;
import java.util.List;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.higgins.lawapp.client.application.dataModel.LawFirm;
import com.higgins.lawapp.client.application.rpc.LawFirmService;

@SuppressWarnings("serial")
public class LawFirmServiceImpl extends RemoteServiceServlet implements LawFirmService {
	private LawFirmDataService lawFirmDataService = new LawFirmDataService();

	public LawFirmServiceImpl()	{
		lawFirmDataService.openConnection();
	}
	
	public void finalize()	throws Throwable {
		lawFirmDataService.closeConnection();
	}
	
	@Override
	public List<String> retrieveRegions() throws IllegalArgumentException {
		List<String> list = new ArrayList<String>();
		list.add("London");
		return list;
	}

	@Override
	public List<String> retrieveAreasOfLaw(String region)
			throws IllegalArgumentException {
		List<String> listOfAreasOfLaw = lawFirmDataService.retrieveAreasOfLaw("London");
		return listOfAreasOfLaw;
	}

	@Override
	public List<String> retrieveLawFirms(String region, String areaOfLaw)
			throws IllegalArgumentException {
		List<String> listOfLawFirms = lawFirmDataService.retrieveLawFirms("London", areaOfLaw);
		return listOfLawFirms;
	}

	@Override
	public List<String> retrieveLawFirmDetails(String region, String areaOfLaw,
			String lawFirm) throws IllegalArgumentException {
		List<String> listOfLawFirmDetails = lawFirmDataService.retrieveLawFirmDetails("London", areaOfLaw, lawFirm);
		return listOfLawFirmDetails;
	}

	@Override
	public List<LawFirm> retrieveLawFirmDetails(String region,
			String areaOfLaw) throws IllegalArgumentException {
		List<LawFirm> listOfLawFirmDetails = lawFirmDataService.retrieveLawFirmDetails("London", areaOfLaw);
		return listOfLawFirmDetails;
	}

	@Override
	public String publish(String coveringLetter) throws IllegalArgumentException {
		List<LawFirm> lawFirmList = lawFirmDataService.retrieveLawFirmsToPublish(coveringLetter);
		
		GoogleSpreadsheetService gsService = new GoogleSpreadsheetService();
		SpreadsheetService service = gsService.openService();
		SpreadsheetEntry entry = gsService.openSpreadSheet(service, "LawFirmSheet");
		gsService.setSpreadsheetData(service, entry, lawFirmList);
		/*
		*/
		return "success";
	}

	@Override
	public List<String> retrieveCoveringLetters()
			throws IllegalArgumentException {
		List<String> coveringLetters = null;
		try	{
			GoogleDocumentService googleDocService = new GoogleDocumentService();
			googleDocService.openService();
			coveringLetters = googleDocService.retrieveCoveringLetters();
		} catch (Exception e)	{
		}
		return coveringLetters;
	}

	@Override
	public Boolean updateLawFirmPublished(Long id, String coveringLetter,
			Boolean published) throws IllegalArgumentException {
		return lawFirmDataService.updateLawFirmPublished(id, coveringLetter, published);
	}
	
}
